package com.ljj.task;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TimePrintingTask implements Runnable {

    private int seconds;

    public TimePrintingTask(int seconds) {
        this.seconds = seconds;
    }

    @Override
    public void run() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < seconds; i++) {
            System.out.println("【任务】当前时间: " + LocalDateTime.now().format(formatter));
            try {
                Thread.sleep(1000); // 每隔1秒打印一次
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                System.out.println("任务被中断");
                break;
            }
        }
        System.out.println("✅ 时间打印任务已完成！");
    }
}